//
//  HJStatusViewModel.swift
//  HJWeiBo
//
//  Created by 夏航军 on 16/9/27.
//  Copyright © 2016年 夏航军. All rights reserved.
//

import UIKit


//viewModelArray 里面存储的是viewMode(即HJStatusViewModel)对象,而对象里面有个属性是status(HJStatus)模型
class HJStatusViewModel: NSObject {

    //通过一个存储型属性给cell提供直接的数据
    //头像数据
    var iconURL: URL?
    //等级
    var memberImage: UIImage?
    //达人
    var avatarImage: UIImage?
    
    //转发
    var reposts_text: String?
    //评论
    var comments_text: String?
    //点赞
    var attitudes_text: String?
    //数据来源
    var sourceText: String?
    
    
    var timeText:String? {
        //Sat Oct 01 16:46:48 +0800 2016
        let dateString = status?.created_at ?? ""
        //1.实例化日期格式对象
        let dateFormater = DateFormatter()
        //2.设置格式
        dateFormater.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
        //设置日期本地化时间,如果不设置,真机上运行会会失败
        dateFormater.locale = Locale(identifier: "en")
        //3.将字符串日期转换为日期对象
        let creatDate = dateFormater.date(from: dateString)
        
        //判断创建的日期是否转换成功
        guard let sinaDate = creatDate else {
            return ""
        }
        
        //使用日历对象进行判断
        let calender = Calendar.current
        
        if isDateInThisYear(targetDate: sinaDate) {
            
            if calender.isDateInToday(sinaDate) {
                //获取当前日期
                let date = Date()
                //返回的是秒
                let detla = date.timeIntervalSince(sinaDate)
                
                if detla < 60 {
                    return "刚刚"
                }else if detla < 3600 {
                    return "\(Int(detla) / 60)分钟前"
                }else{
                    return "\(Int(detla) / 3600)小时前"
                }
                
            }else if calender.isDateInYesterday(sinaDate) {
                //昨天
                dateFormater.dateFormat = "昨天 HH:mm"
                return dateFormater.string(from: sinaDate)
            }else {
                //今年内的其他时间
                dateFormater.dateFormat = "MM-dd"
                return dateFormater.string(from: sinaDate)
            }
        }else {
            //非今年
            dateFormater.dateFormat = "yyyy-MM-dd"
            return dateFormater.string(from: sinaDate)
        }
    }
    
    
    //Mark: 判断创建的日期与当前的日期是否为同一年
    private func isDateInThisYear(targetDate: Date) -> Bool {
        //实例化日期格式对象
        let dateFormatter = DateFormatter()
        //获取当前时间
        let currentDate = Date()
        //重新设置日期格式符
        dateFormatter.dateFormat = "yyyy"
        //将date对象转化为字符串
        //获取创建日期
        let creatYear = dateFormatter.string(from: targetDate)
        //获取当前日期
        let currentYear = dateFormatter.string(from: currentDate)
        
        return creatYear == currentYear
        
        
    }
    
    //处理微博来源
    private func dealSource() -> String {
        //来源打印出来的格式: // <a href=\"http://app.weibo.com/t/feed/3jskmg\" rel=\"nofollow\">iPhone 6s</a>

        let str = status?.source ?? ""
        //定义开始标记
        let startFlag = "\">"
        //定义结束标记
        let endFlag = "</a>"
        
        //查找标记所在的位置或者说这两个标记是否在这段字符串当中
        guard let startRangeIndex = str.range(of: startFlag) ,
        let endRangeIndex = str.range(of: endFlag) else {
            return "位置来源"
        }
        
        //获取拼接的范围
        let startIndex = startRangeIndex.upperBound
        let endIndex = endRangeIndex.lowerBound
        
        //获取范围
        let rangeIndex = startIndex..<endIndex
        let subStr = str.substring(with: rangeIndex)
        
        return subStr
        
    }
    
    //根据是否是转发微博返回不同的配图视图的数据源
    var pictureInfos: [HJStatusPictureInfo]? {
        return status?.retweeted_status == nil ? status?.pic_urls : status?.retweeted_status?.pic_urls
    }

    //Mark:在HJHomeViewModel里面给调用了status的set方法,而我们在这里重写set方法把用户头像/等级/达人的数据进行处理之后存储到模型中,而这个模型作为当前页面viewModel的属性,最后把ViewModel存到数组中
    var status: HJStatus? {
        didSet {
            dealHeadURL()
            dealAvatarImage()
            dealMemberImage()
            reposts_text = dealToolBarTool(count: status?.reposts_count ?? 0, defaultText: "转发")
             comments_text = dealToolBarTool(count: status?.comments_count ?? 0, defaultText: "评论")
            attitudes_text = dealToolBarTool(count: status?.attitudes_count ?? 0, defaultText: "点赞")
            
            //数据来源
            sourceText = dealSource()
        }
    }
    
    /*
     处理底部工具条的数据
     count = 0 ,返回默认值
     大于10000,进行处理
     在这范围直接返回
     */
    //Mark: 工厂设计模式
    func dealToolBarTool(count : Int , defaultText : String) -> String {
        
        if count == 0 {
            return defaultText
        }
        if count > 10000 {
            return "\(CGFloat(count / 10000)/10)万"
        }
        
        return count.description
    }
    
    
    func dealHeadURL() {
        let urlString = status?.user?.avatar_large ?? ""
        iconURL = URL(string: urlString)
    }
    
    func dealAvatarImage() {
        let verified_type = status?.user?.verified_type ?? -1
        switch verified_type {
        case 0:
            avatarImage = #imageLiteral(resourceName: "avatar_vip")
            
        case 2,3,5:
            avatarImage = #imageLiteral(resourceName: "avatar_enterprise_vip")
            
        case 220:
            avatarImage = #imageLiteral(resourceName: "avatar_grassroot")
        default:
            avatarImage = nil
        }
    }
    
    func dealMemberImage() {
        //if let/var 语句中后面赋值对象直接跟',' 则','后面就可以直接使用前面的赋值对像
        
        if let  mbrank = status?.user?.mbrank, mbrank > 0 && mbrank < 7 {
            
            let imageName = "common_icon_membership_level\(mbrank)"
            memberImage = UIImage(named: imageName)

        }
    }
}











